package dp.mobile.panel;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import dp.mobile.eden.R;
import dp.mobile.entity.Entity;
import dp.mobile.entity.Node;
import dp.mobile.entity.generator.Generator;
import dp.mobile.entity.generator.GeneratorData;
import dp.mobile.entity.generator.GeneratorFactory;
import dp.mobile.entity.tower.Tower;
import dp.mobile.entity.tower.TowerData;
import dp.mobile.entity.tower.TowerFactory;
import dp.mobile.game.Game;
import dp.mobile.game.Player;
import dp.mobile.system.Utilities;

public class PanelTouchListener implements OnClickListener {
	private static final String TAG = "Panel Touch Listener";
	
	/// TODO : can improve without using context, but passing resource needed
	public PanelTouchListener(Context context, Game game, RelativeLayout panel) {
		super();
		
		//Init
		mContext	= context;
		mGame		= game;
		mPanel		= panel;
		mPanelLevel = 0;
		mPanelAnimFinish = AnimationUtils.loadAnimation(panel.getContext(), R.anim.transparent_panel_anim_finish);

		mMainImage	= (Button) mPanel.findViewById(R.id.main_image);
		mTextInfo1	= (TextView) mPanel.findViewById(R.id.info1_txtview);
		mTextInfo2	= (TextView) mPanel.findViewById(R.id.info2_txtview); 
		mTextInfo3	= (TextView) mPanel.findViewById(R.id.info3_txtview);;
		
		mAction1 = (Button) mPanel.findViewById(R.id.action1);
		mAction2 = (Button) mPanel.findViewById(R.id.action2);
		mAction3 = (Button) mPanel.findViewById(R.id.action3);
		mAction4 = (Button) mPanel.findViewById(R.id.action4);
		
		mDescription1 	= (TextView) mPanel.findViewById(R.id.description1);
		mDescription2 	= (TextView) mPanel.findViewById(R.id.description2);
		mDescription3 	= (TextView) mPanel.findViewById(R.id.description3);
		mDescription4 	= (TextView) mPanel.findViewById(R.id.description4);
		
		mButton1 = (Button) mPanel.findViewById(R.id.button1);
		mButton2 = (Button) mPanel.findViewById(R.id.button2);
		mButton3 = (Button) mPanel.findViewById(R.id.button3);
		mButton4 = (Button) mPanel.findViewById(R.id.button4);
		
		mDismantleBtn	= (Button) mPanel.findViewById(R.id.dismantle_btn);
	}
	
	@Override
	public void onClick(View view) {
		final Player player = mGame.getPlayer();
		final int x = player.getSelectedTile().x;
		final int y = player.getSelectedTile().y;
		
		// selected
		Entity ent	= mGame.getEntityAt(x, y);
		Tower tower = ent instanceof Tower 	? (Tower)ent : null;
		Node node	= ent instanceof Node 	? (Node)ent : null;
		Generator generator = ent instanceof Generator ? (Generator)ent : null;
		TowerData towerData = null;
		boolean pressed = false;
		
		// level 0
		if (mPanelLevel == 0) {
			switch (view.getId()) {
			case R.id.button1 :
			case R.id.description1 :
			case R.id.action1 : 
				// tanah kosong
				if (ent != null) {
					pressed			= true;
					mPanelLevel 	= 1;
					mSelectedAction = 0;
					
					if (tower != null) {
						//mGame.upgradeTower(tower, 0);
						
					}
					else if (generator != null) {
						//mGame.upgradeGenerator(generator, 0);
					}
				}
				break;
			case R.id.button2 :
			case R.id.description2 :
			case R.id.action2 :
				if (ent != null) {
					pressed			= true;
					mPanelLevel 	= 1;
					mSelectedAction = 1;
					
					if (tower != null) {
						//mGame.upgradeTower(tower, 1);
					}
					else if (generator != null) {
						//mGame.upgradeGenerator(generator, 1);
					}
				}
				
				break;

			case R.id.button3 :
			case R.id.description3 :
			case R.id.action3 :
				if (ent != null) {
					pressed			= true;
					mPanelLevel 	= 1;
					mSelectedAction = 2;
					
					if (tower != null) {
						//towerData = TowerFactory.instance().getTowerData(tower.getData().getUpgrade()[2]);
						//tower.upgrade(towerData, mGame.getElapsedGameTime(), true);
						//mGame.upgradeTower(tower, 2);
					}
					else if (generator != null) {
						//mGame.upgradeGenerator(generator, 2);
					}
				}
				
				break;

			case R.id.button4 :
			case R.id.description4 :
			case R.id.action4 :
				if (ent != null) {
					pressed			= true;
					if (tower != null) {
						
					}
				}
				
				break;
			case R.id.dismantle_btn :
				if (ent != null) {
					if (tower != null) {
						mGame.dismantleTower(tower);
					}
					else if (generator != null) {
						mGame.dismantleGenerator(generator);
					}
				}
				break;
			}
	
			//mPanel.startAnimation(mPanelAnimFinish);
			//mPanel.setVisibility(View.GONE);
			
			/// TODO : 
			/// 1. info damage 
			/// 2. show range
			if (pressed) {
				if (tower != null) {
					towerData = TowerFactory.instance().getTowerData(tower.getData().getUpgrade(mSelectedAction));

					mMainImage.setVisibility(View.VISIBLE);
					mMainImage.setBackgroundDrawable(new BitmapDrawable(towerData.getIcon()));
					mTextInfo1.setText(towerData.getName());
					
					/// TODO : can improve without using context, but passing resource needed
					/// TODO : can improve without new every time doing this
					mAction1.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.electricity)));
					mAction2.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.component0)));
					mAction3.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.component1)));
					mAction4.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.component2)));
					
					mAction1.setVisibility(View.VISIBLE);
					mAction2.setVisibility(View.VISIBLE);
					mAction3.setVisibility(View.VISIBLE);
					mAction4.setVisibility(View.VISIBLE);
					
					final int worth0 = towerData.getWorth(0) - tower.getData().getWorth(0);
					final int worth1 = towerData.getWorth(1) - tower.getData().getWorth(1);
					final int worth2 = towerData.getWorth(2) - tower.getData().getWorth(2);
					
					mTextInfo3.setText("Requirements");
					mDescription1.setText("x " + towerData.getBuildCost());
					mDescription2.setText("x " + worth0);
					mDescription3.setText("x " + worth1);
					mDescription4.setText("x " + worth2);

					mDescription1.setVisibility(View.VISIBLE);
					if (worth0 >= 0) mDescription2.setVisibility(View.VISIBLE);
					if (worth0 >= 0) mDescription3.setVisibility(View.VISIBLE);
					if (worth0 >= 0) mDescription4.setVisibility(View.VISIBLE);
					
					mButton1.setVisibility(View.GONE);
					mButton2.setVisibility(View.GONE);
					mButton3.setVisibility(View.GONE);
					mButton4.setVisibility(View.GONE);
					
					mDismantleBtn.setText("Upgrade");
				} 
				else if (generator != null) {
					GeneratorData generatorData = GeneratorFactory.instance().getGeneratorData(generator.getData().getPowerLevel() + 1, generator.getData().getRangeLevel(), generator.getData().getVoltageLevel());
					//GeneratorData 
					mMainImage.setVisibility(View.VISIBLE);
					
					if (mSelectedAction == 0) {
						mMainImage.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.component0)));
						mTextInfo1.setText("Upgrade Generator's Power");
					} else if (mSelectedAction == 1) {
						mMainImage.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.component1)));
						mTextInfo1.setText("Upgrade Generator's Range");
					} else if (mSelectedAction == 2) {
						mMainImage.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.component2)));
						mTextInfo1.setText("Upgrade Generator's Voltage");
					}
					
					/// TODO : can improve without using context, but passing resource needed
					/// TODO : can improve without new every time doing this
					mAction1.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.electricity)));
					mAction2.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.component0)));
					mAction3.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.component1)));
					mAction4.setBackgroundDrawable(new BitmapDrawable(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.component2)));
					
					mAction1.setVisibility(View.VISIBLE);
					mAction2.setVisibility(View.VISIBLE);
					mAction3.setVisibility(View.VISIBLE);
					mAction4.setVisibility(View.VISIBLE);
					
					final int worth0 = generatorData.getWorth(0) - generator.getData().getWorth(0);
					final int worth1 = generatorData.getWorth(1) - generator.getData().getWorth(1);
					final int worth2 = generatorData.getWorth(2) - generator.getData().getWorth(2);

					mTextInfo3.setText("Requirement");
					mDescription1.setText("x " + generatorData.getBuildCost());
					mDescription2.setText("x " + worth0);
					mDescription3.setText("x " + worth1);
					mDescription4.setText("x " + worth2);

					mDescription1.setVisibility(View.VISIBLE);
					if (worth0 >= 0) mDescription2.setVisibility(View.VISIBLE);
					if (worth0 >= 0) mDescription3.setVisibility(View.VISIBLE);
					if (worth0 >= 0) mDescription4.setVisibility(View.VISIBLE);
					
					mButton1.setVisibility(View.GONE);
					mButton2.setVisibility(View.GONE);
					mButton3.setVisibility(View.GONE);
					mButton4.setVisibility(View.GONE);
					
					mDismantleBtn.setText("Upgrade");
				}
			}
		} 
		// level 1
		else if (mPanelLevel == 1) {
			switch (view.getId()) {
			case R.id.dismantle_btn :
				if (ent != null) {
					if (tower != null) {
						mGame.upgradeTower(tower, mSelectedAction);
					}
					else if (generator != null) {
						mGame.upgradeGenerator(generator, mSelectedAction);
					}
					else if (node != null) {
						mGame.addTower(new Tower(TowerFactory.instance().getBasicTower(), player, x, y, mGame.getElapsedGameTime(), true));
					}
				} else {
					GeneratorData data = GeneratorFactory.instance().getBaseGenerator();
					if (player.getElectricity() >= GeneratorFactory.instance().getBaseBuildCost())
					{
						mGame.addGenerator(new Generator(data,player, x, y, mGame.getElapsedGameTime(), true));
						player.setSelectedTileInvalid();
					}
				}
				break;
			}
	
			mPanel.startAnimation(mPanelAnimFinish);
			mPanel.setVisibility(View.GONE);
		}
	}
	
	public int getPanelLevel() {
		return mPanelLevel;
	}
	
	public void setPanelLevel(int panelLevel) {
		mPanelLevel = panelLevel;
	}

	protected Button			mMainImage;
	protected TextView			mTextInfo1, mTextInfo2, mTextInfo3;
	protected Button			mAction1, mAction2, mAction3, mAction4;
	protected TextView			mDescription1, mDescription2, mDescription3, mDescription4;
	protected Button			mButton1, mButton2, mButton3, mButton4;

	protected Button			mDismantleBtn;
	
	private int				mSelectedAction;
	private int				mPanelLevel;
	private Game			mGame;
	private RelativeLayout	mPanel;
	private Animation		mPanelAnimFinish;
	
	private Context			mContext;
}
